package com.jyw.business.obstacles.domain.vo.web;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.jyw.business.obstacles.domain.ObsOrderAssist;
import com.jyw.business.obstacles.domain.ObsOrderFault;
import com.jyw.business.obstacles.domain.ObsOrderSend;
import com.jyw.common.annotation.Conversion;
import com.jyw.common.annotation.Excel;
import com.jyw.common.core.domain.entity.SysUser;
import com.jyw.common.enums.ConversionType;
import com.jyw.system.domain.vo.SimpleFileVo;
import com.jyw.system.utils.ConversionUtil;
import com.jyw.system.utils.FileUtils;
import com.jyw.system.utils.UserUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.FieldNameConstants;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 沈峻
 * @ClassName AssistMainVO
 * @Description 协作信息
 * @Date 2023/8/17 10:40
 **/
@ApiModel("协作信息")
@Data
@NoArgsConstructor
@FieldNameConstants
public class AssistMainVO {


    /** 协作说明 */
    @ApiModelProperty("协作说明")
    @Excel(name = "协作说明")
    private String assistRemark;

    @ApiModelProperty("发起时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm")
    private Date createTime;

    /** 协作发起人 */
    @ApiModelProperty("协作发起人")
    @Excel(name = "协作发起人")
    @Conversion(value = ConversionType.USER,name = Fields.createUserName)
    private Long createUser;

    @ApiModelProperty("协作发起人名称")
    private String createUserName;

    @ApiModelProperty("协作发起人电话")
    private String createUserPhone;

    @ApiModelProperty("公共附件")
    private List<SimpleFileVo> commonFile;

    @ApiModelProperty("协作工单信息")
    private List<AssistSubVO> subList;

    public AssistMainVO(ObsOrderAssist data, List<ObsOrderFault> childOrder, List<ObsOrderSend> sendList) {
        setAssistRemark(data.getAssistRemark());
        setCreateTime(data.getCreateTime());
        setCreateUser(data.getCreateUser());
        setCommonFile(FileUtils.IdToFile(data.getAssistEnclosureId()));
        SysUser user = UserUtils.getUserById(getCreateUser());
        if(!ObjectUtils.isEmpty(user)){
            setCreateUserPhone(user.getPhonenumber());
        }
        List<AssistSubVO> subList=childOrder.stream()
                //过滤出该协作记录的协作工单
                .filter(fault->data.getAssistOrderNum().indexOf(fault.getOrderNum())>-1)
                //封装为协作工单信息
                .map(fault-> ConversionUtil.conversion(new AssistSubVO(fault,sendList),AssistSubVO.class))
                .collect(Collectors.toList());
        setSubList(subList);
    }
}
